package com.meilai.project.controller.web.personal.mission;


import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.personal.mission.MissionDTO;
import com.meilai.project.dto.personal.mission.MissionOperatorSearchDTO;
import com.meilai.project.dto.personal.mission.MissionSearchDTO;
import com.meilai.project.service.personal.mission.MissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张驰
 * @since 2022-03-24
 */
@Api(value = "任务派发控制器", tags = {"任务派发"})
@RestController
@RequestMapping("api/web/mission")
public class MissionController {
    @Autowired
    private MissionService service;

    @GetMapping("list")
    @ApiOperation(value = "列表")
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @RequestParam(name = "title", required = false) String title,
                             @RequestParam(name = "cur_status", required = false) Integer cur_status) {
        MissionSearchDTO searchDTO = new MissionSearchDTO(title, cur_status);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @GetMapping("my_list")
    @ApiOperation(value = "我发起的任务列表")
    public CommonResult myMissionList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @RequestParam(name = "title", required = false) String title,
                             @RequestParam(name = "cur_status", required = false) Integer cur_status) {
        MissionSearchDTO searchDTO = new MissionSearchDTO(title, cur_status);
        return new CommonResult().success(service.selectMyMissionList(page, page_size, searchDTO));
    }

    @GetMapping("todo_list")
    @ApiOperation(value = "我的待办任务列表")
    public CommonResult myTodoMissionList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                      @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                                      @RequestParam(name = "title", required = false) String title,
                                      @RequestParam(name = "cur_status", required = false) Integer cur_status) {
        MissionOperatorSearchDTO searchDTO = new MissionOperatorSearchDTO();
        return new CommonResult().success(service.selectTodoMissionList(page, page_size, searchDTO));
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除")
    public CommonResult deleted(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.del(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("off")
    @ApiOperation(value = "关闭")
    public CommonResult off(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.off(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("complete")
    @ApiOperation(value = "完成")
    public CommonResult complete(@RequestParam(value = "ids[]") List<Long> ids) {
        if (service.complete(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("operator_complete")
    @ApiOperation(value = "执行者完成任务")
    public CommonResult operator_complete(@RequestParam(value = "ids[]") List<Long> ids,
                                 @RequestParam(value = "complete_memo", required = false) String complete_memo) {
        if (service.operatorComplete(ids, complete_memo)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    public CommonResult add(@RequestBody MissionDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            }
            return new CommonResult().failed("新增失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    public CommonResult edit(@RequestBody MissionDTO dto) {
        try {
            if (service.editOne(dto)) {
                return new CommonResult().success("修改成功");
            }
            return new CommonResult().failed("修改失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }
}
